﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Windows;
using System.Drawing;
namespace SurvivalSolitair
{
    class CardDeck
    {
        private Card[] CardArray = new Card[52];
         //private int[] CardArray = new int[52];
        private int numCards;
        private Random random = new Random();

        public PictureBox myDeckSprite = new PictureBox();

        public List<DropZone> gameField;
        /// <summary>

        /// Maakt een nieuwe 'deck' (boek kaarten) met 52 integers.

        /// </summary>

        public CardDeck(Control Parent,int x,int y,List<DropZone> gameField)
        {
            // Deck uses RankCollated cards 0 - 51

            this.gameField = gameField;
            for (int i = 0; i < 52; i++)
            {
                CardArray[i] = new Card(ShowCard(i),Parent,this.gameField);
                
            }

            numCards = 52;

            myDeckSprite.Image = Image.FromFile(Directory.GetCurrentDirectory() + @"\Data\Sprites\" + "br1.png");
            myDeckSprite.Visible = true;
            myDeckSprite.Width = 75;
            myDeckSprite.Height = 109;
            myDeckSprite.Location = new Point(x, y);
            myDeckSprite.Enabled = true;
            myDeckSprite.Parent = Parent;
            Parent.Controls.Add(this.myDeckSprite);

            myDeckSprite.Click += this.onPullCard;
        }

        public void onPullCard(object o, EventArgs e)
        {
            // Make the new cards visible
            for(int i = 51; i > 48;--i)
            {
                CardArray[i].MoveCard( -(52-i)*18 + 186,50);
                CardArray[i].ToggleVisible();
                CardArray[i].Flip();
                //CardArray[i].

            }
            CardArray[51].ToggleEnabled();
            
            //CardArray[51].MoveCard(50, 60);
            //CardArray[51]


        }

        /// <summary>

        /// Schudden van de 'deck' - algoritme 1
        /// Info over algoritme 1: 2 'willekeurige' kaarten n maal van plaats wisselen

        /// </summary>


        //public void Shuffle1(int n)
        //{
            
        //    Console.Write("Steps:" + n);
            
        //   // Voeg hier uw implementatie toe ...
        //    int firstIndex = -1, secondIndex = -1, Temp = -1;
        //    while (n > 0)
        //    {
        //        firstIndex = random.Next(numCards);
        //        secondIndex = random.Next(numCards);

        //        Temp = CardArray[firstIndex];
        //        CardArray[firstIndex] = CardArray[secondIndex];
        //        CardArray[secondIndex] = Temp;

        //        --n;
        //    }



        //}

        ///// <summary>

        ///// Schudden van de 'deck' - algoritme 2
        ///// Info over algoritme 2: de n eerste kaarten van het deck afnemen en op een willekeurige plaats in het deck terug plaatsen

        ///// </summary>
        
        //public void Shuffle2(int n)
        //{
        
            
        //    // Voeg hier uw implementatie toe ... 
        //    int m = -1, p = Int32.MaxValue;
        //    int[] Hand;

            
        //    Console.Write("Steps:" + n);

        //    while (n > 0)
        //    {
        //        m = random.Next(numCards);
        //        p = random.Next(numCards - m - 1);
                
                
        //        Hand = removeCards(m);
        //        addCards(Hand, p);

        //        --n;
        //    }


            
        //}

        ///// Schudden van de 'deck' - algoritme 3
        ///// Info over algoritme 3:
        ///// Split the deck in 3 and reassemble differently
        ///// </summary>

        //public void Shuffle3(int n)
        //{
        //    // Voeg hier uw implementatie toe ... 
        //    int splitIndex = -1,splitIndex2 = -1;
        //    int[] pile1, pile2,otherPile;

        //    Console.Write("Steps:" + n);

        //    while (n > 0)
        //    {
        //        splitIndex = random.Next(numCards);
        //        splitIndex2 = random.Next(numCards - splitIndex);


        //        pile1 = removeCards(splitIndex);
        //        pile2 = removeCards(splitIndex2);
        //        otherPile = removeCards(numCards-1);


        //        addCards(pile2, 0);
        //        addCards(otherPile, pile2.Length);
        //        addCards(pile1, pile2.Length + otherPile.Length);

        //        --n;
        //    }

        //}

        /// <summary>

        /// Geeft de 'i-de' kaart terug uit de deck

        /// </summary>

        public Card GetCard(int arrayNum)
        {
            if (arrayNum >= 0 && arrayNum <= numCards)
                return CardArray[arrayNum];
            else
                throw (new System.ArgumentOutOfRangeException("arrayNum", arrayNum, "Value must be between 0 and "+ numCards));
        }

        /// <summary>

        /// Verwijder de eerste m kaarten en geef ze terug als een "hand"
        /// We zetten de waarde van de verwijderde kaarten op -1
        /// en verkleinen numCards

        /// </summary>

        public Card[] removeCards(int m)
        {
            Card[] Hand;  
            if (m >= 0 && m < numCards){
                Hand = new Card[m];
                for(int i=0;i<numCards;i++){
                    if(i<m)                    
                        Hand[i] = CardArray[i];
                    else
                        CardArray[i - m] = CardArray[i];
                    if (i >= (numCards - m))
                        CardArray[i] = null;                        
                }                 
            numCards = numCards-m;
            return Hand;    
            }    
            else
                throw (new System.ArgumentOutOfRangeException("m", m, "Value must be between 0 and " + numCards));
        }

        /// <summary>

        /// Voeg hand van m kaarten toe achteraan de deck
        /// en verhoog numCards

        /// </summary>

        public void addCards(Card[] Hand,int Index)
        {
            if (Index > numCards)
                Index = numCards;

            int numHand = Hand.Length;
            if (Index >= 0 )
            {
                // Index is allowed
                for (int i = (CardArray.Length - 1); i  >= 0; --i)
                {
                    // Run through all cards
                    if (i > (Index + numHand))
                    {    //Copy Last part of deck
                        if ((i - numHand) > 0)
                            CardArray[i] = CardArray[i - numHand];
                    }
                    else if (i > Index) 
                    {
                        // input hand
                        CardArray[i] = Hand[i-Index-1];
                    }
                    
                }
                numCards = numCards + numHand;
               
                
            }
            else // Index was out of bounds
                throw (new System.ArgumentOutOfRangeException("Index", Index, "Value must be between 0 and " + numCards));


        }
    
        /// <summary>

        /// Schrijft de waarde van een kaart uit naar het scherm

        /// </summary>

        public string ShowCard (int cardNum)
        {
           int suitNumber,cardValue;
           char suit;
           String card,value;

            suitNumber = (int) Math.Floor(System.Convert.ToDouble(cardNum) / 13);

               switch (suitNumber)
               {
                   case 0:
                       suit = 'C';
                       break;
                   case 1:
                       suit = 'D';
                       break;
                   case 2:
                       suit = 'H';
                       break;
                   case 3:
                       suit = 'S';
                       break;   
                   default: 
                       suit = ' ';
                       break;
               }

               cardValue = cardNum % 13 + 1;

               switch (cardValue)
               {
                   case 11:
                       value = "J";
                       break;
                   case 12:
                       value = "Q";
                       break;
                   case 13:
                       value = "K";
                       break;

                   default:  
                       value = cardValue.ToString();
                       break;
               }

               card = suit.ToString() + value;
               return card;
        }
        /// <summary>

        /// Geeft het aantal kaarten terug in de deck.
       
        /// </summary>

        public int count()
        {
            return numCards;
        }



        //public bool SwapCards(int card1,int card2)
        //{
        //    if( card1 < numCards && card2 < numCards)
        //    {
        //        int temp = CardArray[card1];
        //        CardArray[card1] = CardArray[card2];
        //        CardArray[card2] = temp;
                


        //        return true;
        //    }
        //    else
        //    return false;
        //}
        //public string FindMissing()
        //{
        //    bool[] booleanArray = new bool[52];
        //    string string1 = "";

        //    for (int i = 0; i < numCards; ++i)
        //    {
        //        if (CardArray[i] != -1)
        //        {
        //            booleanArray[CardArray[i]] = true;
        //        }

        //    }
        //    for (int i = 0; i < booleanArray.Length ; ++i)
        //    {
        //        if (!booleanArray[i])
        //        {
        //            string1 += ShowCard(i)+ ",";
        //        }

        //    }

        //    return string1;


        //}
        public bool ResetDeck(Control Parent)
        {
            for (int i = 0; i < 52; i++)
            {
                CardArray[i] = new Card(ShowCard(i),Parent,this.gameField) ;
            }

            numCards = 52;

            return true;
        }



     



    }
}
